include pkg/pkg.mk

## 平台选择
include ../platform/ubuntu/x86_64.mk
# include ../platform/android/armeabi-v7a.mk
# include ../platform/android/arm64-v8a.mk
# include ../platform/android/mips64.mk
# include ../platform/android/x86.mk
# include ../platform/android/x86_64.mk

# 基础路径
ROOT_PATH = $(shell pwd)
LIBS_PATH = $(ROOT_PATH)/libs
PKG_PATH = $(ROOT_PATH)/pkg

# 交叉编译器配置
ARCH ?= $(shell uname -m)
GCC ?= $(CROSS)gcc
GXX ?= $(CROSS)g++
AR ?= $(CROSS)ar
AS ?= $(CROSS)as
NM ?= $(CROSS)nm
RANLIB ?= $(CROSS)ranlib
STRIP ?= $(CROSS)strip
OBJDUMP ?= $(CROSS)objdump
LD ?= $(CROSS)ld

# 编译所有库
all: zlib png libtool jpeg iniparser freetype fethird
	@echo "===== make all success !! ====="

clean:
	@rm -rf $(LIBS_PATH)/*

# 打包所有静态库为 fethird.so 用于动态编译部署
fethird:
	$(GCC) -fPIC -shared -Xlinker -zmuldefs -o $(LIBS_PATH)/lib/libfethird.so \
	$(EXT_CFLAGS) \
	-Wl,--whole-archive \
	$(LIBS_PATH)/lib/libfreetype.a \
	$(LIBS_PATH)/lib/libiniparser.a \
	$(LIBS_PATH)/lib/libjpeg.a \
	$(LIBS_PATH)/lib/libpng16.a \
	$(LIBS_PATH)/lib/libz.a \
	-Wl,--no-whole-archive

freetype-sys-gcc=gcc
ifeq ($(GCC),clang)
freetype-sys-gcc=clang
endif
freetype:
	$(freetype-dpkg) $(PKG_PATH)/$(freetype-name).$(freetype-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(freetype-name) && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--host=$(ARCH) \
		--enable-static \
		--with-pic \
		--with-zlib=no \
		--with-harfbuzz=no \
		CC=$(GCC) \
		LD=$(LD) \
		STRIP=$(STRIP) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		CPPFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LIBPNG_CFLAGS="-I$(LIBS_PATH)/include" \
		LIBPNG_LIBS="-L$(LIBS_PATH)/lib" && \
	sed -i 's%gcc%$(freetype-sys-gcc)%' ./builds/unix/unix-cc.mk && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(freetype-name)

iniparser:
	@$(iniparser-dpkg) $(PKG_PATH)/$(iniparser-name).$(iniparser-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(iniparser-name) && \
	make -j4 CC=$(GCC) AR=$(AR) ADDITIONAL_CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC" && \
	mkdir -p $(LIBS_PATH)/lib $(LIBS_PATH)/include && \
	cp libiniparser.a $(LIBS_PATH)/lib/ && \
	cp libiniparser.so* $(LIBS_PATH)/lib/ && \
	cp src/iniparser.h $(LIBS_PATH)/include/ && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(iniparser-name)

jpeg:
	@rm -rf $(LIBS_PATH)/jpeg-* && \
	$(jpeg-dpkg) $(PKG_PATH)/$(jpeg-name).$(jpeg-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/jpeg-6b && \
	sed -i 's%\x0D%%' ./configure && \
	sed -i 's%\x0D%%' ./ltconfig && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--target=$(ARCH) \
		--host=$(ARCH) \
		--enable-static \
		--enable-shared && \
	cp $(LIBS_PATH)/bin/libtool ./ && \
	sed -i 's%CC= gcc%CC= $(GCC)%' ./Makefile && \
	sed -i 's%AR= ar%AR= $(AR)%' ./Makefile && \
	sed -i 's%AR2= ranlib%AR2= $(RANLIB)%' ./Makefile && \
	sed -i 's%CFLAGS=%CFLAGS= $(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC %' ./Makefile && \
	sed -i 's%LDFLAGS=%LDFLAGS= $(EXT_CFLAGS) %' ./Makefile && \
	mkdir -p $(LIBS_PATH)/man/man1 && \
	make -j4 && make install && \
	sed -i '/^#define JPEGLIB_H/a\#include <stddef.h>' ../include/jpeglib.h && \
	cd - && \
	rm -rf $(LIBS_PATH)/jpeg-*

libtool-host=$(ARCH)
ifeq ($(ARCH),aarch64)
libtool-host=arm
endif
libtool:
	@$(libtool-dpkg) $(PKG_PATH)/$(libtool-name).$(libtool-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(libtool-name) && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--host=$(libtool-host) \
		--with-pic \
		--disable-ltdl-install \
		CC=$(GCC) \
		CXX=$(GXX) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		CPPFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(libtool-name)

# 生成.so时 --enable-shared 反则 --enable-shared=no
png:
	$(png-dpkg) $(PKG_PATH)/$(png-name).$(png-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(png-name) && \
	./configure \
		-prefix=$(LIBS_PATH) \
		--host=$(ARCH) \
		--enable-static \
		--enable-shared \
		--with-pic \
		CC=$(GCC) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib -l:libz.a" \
		CPPFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(png-name)

# 生成.so时删去 --static
zlib:
	$(zlib-dpkg) $(PKG_PATH)/$(zlib-name).$(zlib-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(zlib-name) && \
	sed -i '1i\CC=$(GCC)' ./configure && \
	sed -i '1i\CFLAGS=$(EXT_CFLAGS)' ./configure && \
	./configure --prefix=$(LIBS_PATH) && \
	sed -i 's%AR=ar%AR=$(AR)%' ./Makefile && \
	sed -i 's%RANLIB=ranlib%RANLIB=$(RANLIB)%' ./Makefile && \
	sed -i 's%CFLAGS=%CFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC %' ./Makefile && \
	sed -i 's%SFLAGS=%SFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) %' ./Makefile && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(zlib-name)

